package com.wontube.smr.cron;

import java.util.HashSet;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wontube.core.cron.IJobs;
import com.wontube.smr.bo.sites.vevo.VevoChartSpider;
import com.wontube.smr.bo.sites.vevo.VevoVideoBO;



/** 
 * @author tanggf(2012-4-6)
 *
 */
public class ObtainVevoJobs implements IJobs {
	private final static Logger logger = LoggerFactory.getLogger(ObtainVevoJobs.class);

	@Override
	public String name() {
		return "obtain-vevo";
	}

	//可能参数: -daily_chart, -weekly_chart, -all_time_chart, -staff_pick,  -music_video
	@Override
	public IJobs execute(String[] args) {
		//防止参数重复
		Set<String> set = new HashSet<String>();
		for (String arg : args) {
			set.add(arg);
		}
		
		if(set.contains("-daily_chart")){
			this.obtainDailyChart();
		}
		
		if(set.contains("-staff_pick")){
			this.obtainStaffPicks();
		}
		
		if(set.contains("-weekly_chart")){
			this.obtainWeeklyChart();
		}
		
		if(set.contains("-all_time_chart")){
			this.obtainAllTimeChart();
		}
		
		//====
		if(set.contains("-music_video")){
			this.obtainVideos();
		}
		
		if(set.contains("-music_genre")){
			this.updateMusicGenres();
		}
		if(set.contains("-music_today")){
			this.obtainTodayMusics();
		}
		if(set.contains("-music_week")){
			this.obtainWeekMusics();
		}
		if(set.contains("-music_recent")){
			this.obtainRecentMusics();
		}		
		
		return this;
	}
	
	private void obtainAllTimeChart() {
		long start = System.currentTimeMillis();
		int count = 0;
		try {
			count += VevoChartSpider.grabAllTimeTopVideos();
			count += VevoChartSpider.grabAllTimeTopArtists();
		} catch (Exception e) {
			logger.error("errors happen when grab vevo all time video/artist", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("obtain {} vevo all time chart items, cost {} s", count, cost);		
	}

	private void obtainWeeklyChart() {
		long start = System.currentTimeMillis();
		int count = 0;
		try {
			count += VevoChartSpider.grabWeeklyTopVideos();
			count += VevoChartSpider.grabWeeklyTopArtists();
		} catch (Exception e) {
			logger.error("errors happen when grab vevo weekly video/artist", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("obtain {} vevo weekly chart items, cost {} s", count, cost);
	}

	private void obtainStaffPicks() {
		long start = System.currentTimeMillis();
		int count = 0;
		try {
			count = VevoChartSpider.grabTopStaffPicks();
		} catch (Exception e) {
			logger.error("errors happen when grab vevo staff picks	 music", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("obtain {} vevo staff picks chart items, cost {} s", count, cost);
	}

	private void obtainDailyChart() {
		long start = System.currentTimeMillis();
		int count = 0;
		try {
			count += VevoChartSpider.grabDailyTopVideos();
			count += VevoChartSpider.grabDailyTopArtists();
		} catch (Exception e) {
			logger.error("errors happen when grab vevo daily video/artist", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("obtain {} vevo daily chart items, cost {} s", count, cost);
	}
	
	private void obtainVideos() {
		long start = System.currentTimeMillis();	
		int count = 0;
		try {
			count = VevoVideoBO.saveAllMusicVideos();
		} catch (Exception e) {
			logger.error("errors happen when grab vevo music video", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("obtain {} Vevo music videos, cost {} s", count, cost);		
	}
	
	private void updateMusicGenres() {
		long start = System.currentTimeMillis();	
		int count = 0;
		try {
			count = VevoVideoBO.updateMusicGenres();
		} catch (Exception e) {
			logger.error("errors happen when update Vevo music video genres", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("update {} Vevo music video genres, cost {} s", count, cost);		
	}
	
	private void obtainTodayMusics(){
		long start = System.currentTimeMillis();	
		int count = 0;
		try {
			count = VevoVideoBO.updateTodayMusics();
		} catch (Exception e) {
			logger.error("errors happen when obtain Vevo Today Musics", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("obtain {} Vevo Today Musics, cost {} s", count, cost);	
	}
	
	private void obtainWeekMusics(){
		long start = System.currentTimeMillis();	
		int count = 0;
		try {
			count = VevoVideoBO.updateWeeklyMusics();
		} catch (Exception e) {
			logger.error("errors happen when obtain Vevo Week Musics", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("obtain {} Vevo Week Musics, cost {} s", count, cost);	
	}
	
	private void obtainRecentMusics(){
		long start = System.currentTimeMillis();	
		int count = 0;
		try {
			count = VevoVideoBO.updateRecentMusics();
		} catch (Exception e) {
			logger.error("errors happen when obtain Vevo Recent Musics", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("obtain {} Vevo Recent Musics, cost {} s", count, cost);	
	}
}
